package com.fly.office.word.util;

import com.fly.office.word.model.table.TableModel;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 表格工具类
 */
public class TableUtil {

    private static Logger logger = LoggerFactory.getLogger(TableUtil.class);

    private static String keyModel = "${key}";

    /**
     * 处理段落文字
     *
     * @param doc
     * @throws InvalidFormatException
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void doParagraphs(XWPFDocument doc, TableModel model) throws Exception {
        /**----------------------------处理段落------------------------------------**/
        List<XWPFParagraph> paragraphList = doc.getParagraphs();
        if (paragraphList != null && paragraphList.size() > 0) {
            for (XWPFParagraph paragraph : paragraphList) {
                List<XWPFRun> runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    String text = run.getText(0);
                    if (text != null) {
                        // 动态表格
                        if (text.contains(keyModel.replace("key",model.getKey()))) {
                            run.setText("", 0);
                            XmlCursor cursor = paragraph.getCTP().newCursor();
                            XWPFTable tableOne = doc.insertNewTbl(cursor);


                            // 设置表格宽度
                            tableOne.setWidth(8500);

                            // 表格第一行，对于每个列，必须使用createCell()，而不是getCell()，因为第一行嘛，肯定是属于创建的，没有create哪里来的get呢
                            //行
                            XWPFTableRow tableOneRowOne = tableOne.getRow(0);
                            for(int i=0;i<model.getTitles().size();i++)
                            {

                                if(i==0)
                                {
                                    PoiWordTools.setWordCellSelfStyle(tableOneRowOne.getCell(i), model.getTitleStyle().getFontName(), model.getTitleStyle().getFontSize(), model.getTitleStyle().getFontBlod(), model.getTitleStyle().getAlignment(), model.getTitleStyle().getVertical(), model.getTitleStyle().getFontColor(), model.getTitleStyle().getBgColor(), "10%", model.getTitles().get(i));
                                }else{
                                    PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), model.getTitleStyle().getFontName(), model.getTitleStyle().getFontSize(), model.getTitleStyle().getFontBlod(), model.getTitleStyle().getAlignment(), model.getTitleStyle().getVertical(), model.getTitleStyle().getFontColor(), model.getTitleStyle().getBgColor(), "20%", model.getTitles().get(i));
                                }
                            }

                            XWPFTableRow tableOneRowTwo = null;
                            for(Map<String,String> data:model.getDataList())
                            {
                                //行
                                tableOneRowTwo = tableOne.createRow();
                                int num = 0;
                                for(String value : data.values()){
                                    if(num==0)
                                    {
                                        PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(num), model.getDataStyle().getFontName(), model.getDataStyle().getFontSize(), model.getDataStyle().getFontBlod(), model.getDataStyle().getAlignment(), model.getDataStyle().getVertical(), model.getDataStyle().getFontColor(), model.getDataStyle().getBgColor(), "10%", value);
                                    }else{
                                        PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(num), model.getDataStyle().getFontName(), model.getDataStyle().getFontSize(), model.getDataStyle().getFontBlod(), model.getDataStyle().getAlignment(), model.getDataStyle().getVertical(), model.getDataStyle().getFontColor(), model.getDataStyle().getBgColor(), "20%", value);
                                    }
                                    num++;
                                }
                            }
                        }

                    }
                }
            }
        }
    }

}
